#ifndef CHARTFORM_H
#define CHARTFORM_H

#include <QWidget>
#include <QRadioButton>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QChart>
QT_CHARTS_USE_NAMESPACE

#include <QHBoxLayout>


namespace Ui {
class chartForm;
}

enum AXIS{
    X_AXIS=1,
    Y_AXIS=2
};

class chartForm : public QWidget
{
    Q_OBJECT

public:
    explicit chartForm(QWidget *parent = nullptr);
    ~chartForm();

    void addSeries(QString SeriesTitle, QString Axis_X_name, QString Axis_Y_name);

    void addPoint(QString SeriesTitle, QPointF Point);
    void addPoints(QString SeriesTitle, QList<QPointF> Points);
    void showSeries(QString SeriesTitle);

    void clearSeries(QString SeriesTitle);
    void clearAll();

    void setMeaaage(QString);

    void shortShow(int pointsCounts=100);
    void longShow();
    void saveAllPoints(const QString defualtName = "datafile");
    void showBar(bool flag = false);

    void getSeries(QString &SeriesTitle, QLineSeries *&ss);
    void getCurrentSeries(QString &SeriesTitle,QLineSeries *&ss);

    void setSeriesPen(QString SeriesTitle, QPen pen);
    void setTitleFont(AXIS, QFont font);
    void setTickCount(AXIS, int TickCount =6);
    void setMinorTickCount(AXIS, int MinorTickCount=4);
    void setLabelsFont(AXIS, QFont font);
    void setLabelFormat(AXIS, int LabelFormat = 3);
    void setLabelsAngle(AXIS, int Angle =  -60);

    QMap<QString, QRadioButton*> buttonMap;


private:
    Ui::chartForm *ui;
    QHBoxLayout *layout = new QHBoxLayout;
    QLineSeries *factSeries = new QLineSeries();
    QMap<QString, QLineSeries*> Series;
    QMap<QString, QStringList> nameMap;


    QChart *chart = new QChart();
    QValueAxis *xAxis = new QValueAxis;
    QValueAxis *yAxis = new QValueAxis;

    void updateAxisRanges(QLineSeries *series) {
        if (series->count() == 0) return;

        // 获取当前系列的数据范围
        qreal minX = series->at(0).x();
        qreal maxX = series->at(0).x();
        qreal minY = series->at(0).y();
        qreal maxY = series->at(0).y();

        for (int i = 1; i < series->count(); ++i) {
            const QPointF &point = series->at(i);
            minX = qMin(minX, point.x());
            maxX = qMax(maxX, point.x());
            minY = qMin(minY, point.y());
            maxY = qMax(maxY, point.y());
        }

        // 设置轴的范围
        xAxis->setRange(minX, maxX);
        yAxis->setRange(minY, maxY);
    }

    int showCounts = 8000;

    void showSeries_private(QString SeriesTitle);

};

#endif // CHARTFORM_H
